<template>
  <div class="app-container">
    <div>
      <el-form :model="query" ref="queryRef" :inline="true" size="small">
        <el-form-item label="手机号">
          <el-input v-model="query.mobile" clearable></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            v-model="query.status"
            placeholder="请选择状态"
            clearable
            filterable
          >
            <el-option
              v-for="item in statusList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户状态">
          <el-select
            v-model="query.userType"
            placeholder="请选择用户状态"
            clearable
            filterable
          >
            <el-option
              v-for="item in userTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="list" border stripe>
      <el-table-column label="#" type="index" />
      <el-table-column label="用户类型" prop="userType">
        <template slot-scope="{ row }">
          {{ row.userType === 1 ? '投资人' : '借款人' }}
        </template>
      </el-table-column>
      <el-table-column label="手机号" prop="mobile" />
      <el-table-column label="用户姓名" prop="name" />
      <el-table-column label="身份证号" prop="idCard" />
      <el-table-column label="用户积分" prop="integral" />
      <el-table-column label="注册时间" prop="createTime">
        <template slot-scope="{ row }">
          {{ row.createTime | formateDate }}
        </template>
      </el-table-column>
      <el-table-column label="绑定状态" prop="bindStatus">
        <template slot-scope="{ row }">
          <el-tag v-if="row.bindStatus === 0" type="info" size="small"
            >未绑定</el-tag
          >
          <el-tag v-else-if="row.bindStatus === 1" type="success" size="small"
            >绑定成功</el-tag
          >
          <el-tag v-else-if="row.bindStatus === -1" type="danger" size="small"
            >绑定失败</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="用户状态" prop="status">
        <template slot-scope="{ row }">
          <el-tag v-if="row.status === 1" type="success" size="small"
            >正常</el-tag
          >
          <el-tag v-else-if="row.status === 0" type="danger" size="small"
            >锁定</el-tag
          >
        </template>
      </el-table-column>

      <el-table-column label="操作" width="180">
        <template slot-scope="{ row }">
          <el-button
            v-if="row.status === 1"
            type="primary"
            size="mini"
            @click="handleLock(row.id, 0)"
            >锁定</el-button
          >
          <el-button
            v-else
            type="danger"
            size="mini"
            @click="handleLock(row.id, 1)"
            >解锁</el-button
          >
          <el-button type="info" size="mini" @click="showLog(row.id)"
            >登录日志</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="text-align: right; margin-top: 10px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.page"
      :page-sizes="[10, 50, 100]"
      :page-size="query.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <el-dialog title="登录日志" :visible.sync="logDialog" width="40%">
      <div>
        <el-table :data="logList" border stripe>
          <el-table-column label="IP" prop="ip" />
          <el-table-column label="登录时间" prop="createTime" />
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import commonApi from '@/api/common'
import getQueryPageList from '@/mixins/getQueryPageList'
import userInfoApi from '@/api/user-info'
export default {
  name: 'userList',
  data() {
    return {
      logDialog: false,
      apiName: '/admin/core/userInfo',
      queryPageListPost: commonApi.queryPageListPost,
      list: [],
      total: 0,
      query: {
        page: 1,
        limit: 10,
        mobile: '',
        status: '',
        userType: '',
      },
      statusList: [
        { value: '', label: '-请选择-' },
        { value: 1, label: '正常' },
        { value: 0, label: '锁定' },
      ],
      userTypeList: [
        { value: '', label: '-请选择-' },
        { value: 1, label: '投资人' },
        { value: 2, label: '借款人' },
      ],
      logList: [],
    }
  },
  mixins: [getQueryPageList],
  created() {
    this.getQueryPageList(this.apiName, this.query)
  },
  methods: {
    handleQuery() {
      this.getQueryPageList(this.apiName, this.query)
    },
    async handleLock(id, status) {
      let data = {
        id,
        status,
      }

      try {
        let res = await userInfoApi.lock(data)
        this.$message.success(res.message)
        this.getQueryPageList(this.apiName, this.query)
      } catch (error) {
        return this.$message.error(error)
      }
    },

    handleCurrentChange(page) {
      this.query.page = page
      this.getQueryPageList(this.apiName, this.query)
    },
    handleSizeChange(limit) {
      this.query.limit = limit
      this.getQueryPageList(this.apiName, this.query)
    },
    async showLog(userId) {
      try {
        let res = await userInfoApi.logInfo(userId)
        this.logList = res.data.list
      } catch (error) {
        console.log(error);
      }
      this.logDialog = true
    },
  },
}
</script>

<style lang="scss" scoped>
</style>